<?php

/**
 *  extention.func.php 用户自定义函数库
 *
 * @copyright			(C) 2005-2010 PHPCMS
 * @license				http://www.phpcms.cn/license/
 * @lastmodify			2010-10-27
 */
function getVoteCatId($siteid = 1) {
	$catid = 104;
	return $catid;
}
function weather() {
	define('WEATHER_CACHE',CACHE_PATH.'configs\\');
	define('WEATHER_CACHE_FILE',WEATHER_CACHE.'weather.php');
	if (file_exists(WEATHER_CACHE_FILE)) { 
		$weather=null;
		require_once WEATHER_CACHE_FILE;
		if($weather==null){
			make_weather_cache();
		}else{
			if($weather['day']==date('Ymd',time())){
				echo $weather['content'];
			}else{
				make_weather_cache();
			}
		}
	} else { 
		make_weather_cache();
	} 
//	require_once CACHE_MODEL_PATH.'content_output.class.php';
}
function make_weather_cache(){
		$data="";
		$ch = curl_init();
		$timeout = 30;
		curl_setopt($ch, CURLOPT_URL, "http://113.108.239.113/data/101210401.html");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		$data = curl_exec($ch);
		curl_close($ch);
		$data = json_decode($data, true);
		
		//113.108.239.113   113.108.239.114    113.108.239.116  113.108.239.117 113.108.239.118 113.108.239.119
		
		if(empty($data['weatherinfo']['img_title1'])){
			$ch = curl_init();
			$timeout = 30;
			curl_setopt($ch, CURLOPT_URL, "http://113.108.239.114/data/101210401.html");
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
			$data = curl_exec($ch);
			curl_close($ch);
			$data = json_decode($data, true);
			if (empty($data['weatherinfo']['img_title1'])){
				$ch = curl_init();
				$timeout = 30;
				curl_setopt($ch, CURLOPT_URL, "http://113.108.239.116/data/101210401.html");
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
				$data = curl_exec($ch);
				curl_close($ch);
				$data = json_decode($data, true);
				if (empty($data['weatherinfo']['img_title1'])){
					$ch = curl_init();
					$timeout = 30;
					curl_setopt($ch, CURLOPT_URL, "http://113.108.239.117/data/101210401.html");
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
					curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
					$data = curl_exec($ch);
					curl_close($ch);
					$data = json_decode($data, true);
					if (empty($data['weatherinfo']['img_title1'])){
						$ch = curl_init();
						$timeout = 30;
						curl_setopt($ch, CURLOPT_URL, "http://113.108.239.118/data/101210401.html");
						curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
						curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
						$data = curl_exec($ch);
						curl_close($ch);
						$data = json_decode($data, true);
						if (empty($data['weatherinfo']['img_title1'])){
							$ch = curl_init();
							$timeout = 30;
							curl_setopt($ch, CURLOPT_URL, "http://113.108.239.119/data/101210401.html");
							curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
							curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
							$data = curl_exec($ch);
							curl_close($ch);
							$data = json_decode($data, true);
								
						}
							
					}
						
				}
					
			}
		}

		
		$content = "<strong>今天：</strong>" . $data['weatherinfo']['city'] . "&nbsp;" . $data['weatherinfo']['img_title1'] . "&nbsp;";
		$content .= "<span style=\"color:#cc3300\">" . $data['weatherinfo']['temp1'] . "</span>" . "&nbsp;" . $data['weatherinfo']['wind1'] . "&nbsp;";
		$content .= "<strong>明天：</strong>" . $data['weatherinfo']['city'] . "&nbsp;" . $data['weatherinfo']['img_title2'];
		$content .= "<span style=\"color:#cc3300\">" . $data['weatherinfo']['temp2'] . "</span>" . "&nbsp;" . $data['weatherinfo']['wind2'] . ";&nbsp;";
		$content .= "<strong>后天：</strong>" . $data['weatherinfo']['city'] . "&nbsp;" . $data['weatherinfo']['img_title3'];
		$content .= "<span style=\"color:#cc3300\">" . $data['weatherinfo']['temp3'] . "</span>" . $data['weatherinfo']['wind3'];
		$weather['content']=$content;
		$weather['day']=date('Ymd',time());
		$weather="<?php return array(\n"."\t'day'=>'".$weather['day']."',\n \t'content'=>'".$weather['content']."',\n\t);\n?>";
		file_put_contents(WEATHER_CACHE_FILE,$weather);
		chmod(WEATHER_CACHE_FILE,0777);
		echo $content;
}

/**
 * 获取公网IP地址
 * @return Ambigous <string, unknown>
 */
function get_onlineip() {
	$onlineip = '';
	if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
		$onlineip = getenv('HTTP_CLIENT_IP');
	} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
		$onlineip = getenv('HTTP_X_FORWARDED_FOR');
	} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
		$onlineip = getenv('REMOTE_ADDR');
	} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
		$onlineip = $_SERVER['REMOTE_ADDR'];
	}
	return $onlineip;
}

/**
 * 获取本机真实IP地址:即192.168.1.88 此类型
 * @return string
 */
function getLocalIP() {
	$preg = "/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\Z/";
	//获取操作系统为win2000/xp、win7的本机IP真实地址
	exec("ipconfig", $out, $stats);
	if (!empty($out)) {
		foreach ($out AS $row) {
			if (strstr($row, "IP") && strstr($row, ":") && !strstr($row, "IPv6")) {
				$tmpIp = explode(":", $row);
				if (preg_match($preg, trim($tmpIp[1]))) {
					return trim($tmpIp[1]);
				}
			}
		}
	}
}

/**
 获取客户端访问地址
 **/
function getRealIpAddr(){
	if (!empty($_SERVER['HTTP_CLIENT_IP'])){//check ip from share internet
		$ip=$_SERVER['HTTP_CLIENT_IP'];
	}
	elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){//to check ip is pass from proxy
		$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
	}else{
		$ip=$_SERVER['REMOTE_ADDR'];
	}
	return $ip;
}

function get_real_ip(){
	if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknow")){
		$ip = getenv("HTTP_CLIENT_IP");
	}else if(getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknow")){
		$ip = getenv("HTTP_X_FORWARDED_FOR");
	}else if(getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknow")){
		$ip = getenv("REMOTE_ADDR");
	}else if(isset($_SERVER["REMOTE_ADDR"]) && $_SERVER["REMOTE_ADDR"] && strcasecmp($_SERVER["REMOTE_ADDR"],"unknow")){
		$ip = $_SERVER["REMOTE_ADDR"];
	}else{
		$ip = "unknow";
	}
	return $ip;
}
?>